package com.example.sys.ai.dto;

import com.alibaba.fastjson.JSONObject;
import com.example.core.utils.StringUtils;
import lombok.Data;

import java.io.Serializable;

@Data
public class BusinessLicense implements Serializable {

    public static final String WORDS_RESULT = "words_result";

    public static final String BUSINESS = "经营范围";

    public static final String LEGAL = "法人";

    public static final String NO = "证件编号";

    public static final String NAME = "单位名称";

    public static final String VALIDITY = "核准日期";

    public static final String CODE = "社会信用代码";

    public static final String ADDRESS = "地址";

    public static final String TYPE = "类型";

    public static final String WORDS = "words";

    //经营范围
    private String business;

    //法人
    private String legal;

    //证件编号
    private String No;

    //单位名称
    private String name;

    //核准日期
    private String validity;

    //社会信用代码
    private String code;

    //地址
    private String address;

    //类型
    private String type;

    /***
     * 解析营业执照信息
     * @param json
     * @return
     */
    public static BusinessLicense toLicense(String json) {
        BusinessLicense license = new BusinessLicense();
        if (StringUtils.isNotEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (jsonObject != null && jsonObject.containsKey(WORDS_RESULT)) {
                jsonObject = jsonObject.getJSONObject(WORDS_RESULT);

                //营业范围
                if (jsonObject.containsKey(BUSINESS)) {
                    String str = jsonObject.getJSONObject(BUSINESS).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setBusiness(str);
                    }
                }

                //法人
                if (jsonObject.containsKey(LEGAL)) {
                    String str = jsonObject.getJSONObject(LEGAL).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setLegal(str);
                    }
                }

                //证件编号
                if (jsonObject.containsKey(NO)) {
                    String str = jsonObject.getJSONObject(NO).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setNo(str);
                    }
                }

                //单位名称
                if (jsonObject.containsKey(NAME)) {
                    String str = jsonObject.getJSONObject(NAME).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setName(str);
                    }
                }

                //核准日期
                if (jsonObject.containsKey(VALIDITY)) {
                    String str = jsonObject.getJSONObject(VALIDITY).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setValidity(str);
                    }
                }

                //社会信用代码
                if (jsonObject.containsKey(CODE)) {
                    String str = jsonObject.getJSONObject(CODE).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setCode(str);
                    }
                }

                //地址
                if (jsonObject.containsKey(ADDRESS)) {
                    String str = jsonObject.getJSONObject(ADDRESS).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setAddress(str);
                    }
                }

                //类型
                if (jsonObject.containsKey(TYPE)) {
                    String str = jsonObject.getJSONObject(TYPE).getString(WORDS);
                    if (StringUtils.isNotEmpty(str)) {
                        license.setType(str);
                    }
                }

            }
        }
        return license;
    }
}
